package com.study.demo.aop.anno;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * @author: elvin
 * @time: 2019-08-05 15:46
 * @desc:
 **/
@Aspect
public class CalculatorAspect {

    //配置切入点
    @Pointcut("execution(public int com.study.demo.aop.anno.Calculator.*(..))")
    public void pointCut() {
    }

    //执行目标方法之前执行
    @Before("pointCut()")
    public void before(JoinPoint jp) {
        Object[] args = jp.getArgs();
        System.out.println(jp.getSignature().getName() + " @Before:" + Arrays.asList(args));
    }

    @AfterReturning(value = "pointCut()", returning = "res")
    public void afterReturn(JoinPoint jp, Object res) {
        System.out.println(jp.getSignature().getName() + " @AfterReturning:" + res);
    }

    @AfterThrowing(value = "pointCut()", throwing = "e")
    public void exception(JoinPoint jp, Exception e) {
        System.out.println("" + jp.getSignature().getName() + " @AfterThrowing:" + e);
    }

    @After("pointCut()")
    public void after(JoinPoint jp) {
        Object[] args = jp.getArgs();
        System.out.println(jp.getSignature().getName() + " @After:" + Arrays.asList(args));
    }
}
